<?php

ini_set('memory_limit', '-1');
define('root', bn_rootdir());
define('basepath', bn_root());
$timezone='Asia/Tehran';
date_default_timezone_set($timezone);

/**
 * bn_get_relative_path()
 * return relative path of absolute path
 * @return boolean
 */
function bn_get_relative_path()
{
    $str = '';
    for ($i = 0; $i < substr_count(dirname($_SERVER['REQUEST_URI']), '/'); $i++)
    {
        $str .= '../';
    }
    if ($str == '')
        return './';
    else
        return $str;
}

/**
 * bn_add_endpath_slash()
 * 
 * @param mixed $path
 * @return string
 */
function bn_add_endpath_slash($path)
{
    return rtrim($path,'/').'/';
}

function bn_add_begpath_slash($path)
{
    return ltrim($path,'/').'/';    
}

function bn_check_fileEXT($file, $EXTlist = array())
{
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    
    if (!is_array($EXTlist))
        $EXTlist = explode(',', $EXTlist);

    foreach ($EXTlist as $list)
    {
        if ($ext == strtolower($list))
            return true;
    }
    return false;
}

function bn_root($path='')
{   
    return str_replace('\\','/', dirname(__dir__ ));
}
//fix root dir for mor than 1 subdir root
function bn_rootdir($path='')
{
    return str_replace($_SERVER['DOCUMENT_ROOT'],'',bn_root());
}

function __autoload($classname)
{
    include_once (bn_get_dir_libs() . "/$classname.php");
}

function bn_redirect($refer)
{echo "reffer";
    ob_start();
    header("Location: $refer");
    die;
}

function bn_redirect_postdata($refer,$data=array())
{
    ob_start();
    if(!empty($data))
    { 
        echo "\n<form id='form1' method='post' action='$refer'>";
        foreach($data as $key=>$val)
        {
           echo "\n<input type='hidden' name='$key' value='$val'/>";  
        } 
        echo "\n</form>";
        echo "\n<script>
             document.getElementById('form1').submit();
             </script>" ; 
    }
    die();
}

function bn_is_absolute_path($path)
{
    if ($path{0} == '/' || preg_match('|^[a-zA-Z]+://|', $path))
    {
        return true;
    } else
    {
        return false;
    }
}

function bn_Get_Current_page()
{
    return $_SERVER["SCRIPT_NAME"];
}

function bn_Get_sitename()
{
    return ($_SERVER['HTTPS'] == 'on')?"https://" . $_SERVER['HTTP_HOST']:"http://" . $_SERVER['HTTP_HOST'];
}

function bn_get_siteroot()
{
    return bn_Get_sitename().bn_rootdir();
}

function bn_Get_current_URL()
{
    return bn_Get_sitename() . $_SERVER['REQUEST_URI'];
}
//__________________________________________________________________________
function bn_get_dir_libs($path='')
{
    return basepath . "/libs$path";
}

function bn_get_dir_template($path='')
{
    return basepath . "/template$path";
}

function bn_get_dir_gadget($path='')
{
    return basepath . "/gadgets$path";
}

function bn_get_dir_module($path='')
{
    return basepath . "/modules$path";
}

function bn_get_dir_lang($path='')
{
    return basepath . "/lang$path";
}

function bn_get_dir_upload($path='')
{
   return  basepath."/files/upload/$path";
}

function bn_dir_files($path='')
{
 return  basepath."/files/$path";   
}

function bn_get_dir_css($path='')
{
    return rtrim(bn_get_relative_path(),'/'). root . "/template/css$path";
}

function bn_get_dir_jquery($path='')
{
    return rtrim(bn_get_relative_path(),'/'). root . "/template/jquery$path";
}

function bn_get_dir_imgs($path='')
{
    return rtrim(bn_get_relative_path(),'/'). root . "/template/imgs$path";
}
//_________________________________________________________________________
function bn_include_lib($libname)
{
    include_once (bn_get_dir_libs("/$libname.php"));
}

function bn_include_header($headername)
{
    include_once (bn_get_dir_template("/$headername.php"));
}

function bn_include_lang($langname)
{
    include_once (bn_get_dir_lang("/$langname.php"));
}

function bn_jquery_path($jsname)
{
    echo bn_get_dir_jquery("/$jsname.js");
}

function bn_css_path($csname)
{
    echo bn_get_dir_css("/$csname.css");
}

function bn_smalthumb_path($filename='')
{
   return bn_get_siteroot()."/files/thumbnil/smalthumb/$filename"; 
}

function bn_dir_smalthumb($filename='')
{
    return root."/files/thumbnil/smalthumb/$filename";
}

function bn_largthumb_path($filename='')
{
   return bn_get_siteroot()."/files/thumbnil/smalthumb/$filename"; 
}

function bn_dir_largthumb($filename='')
{
  return bn_get_siteroot()."/files/thumbnil/largthumb/$filename";   
}
?>